/*
 * Content: sleep()-yield()用法
 * --------------------------------------------------
 * Java多线程的sleep（休眠）: https://blog.csdn.net/weixin_47556601/article/details/123087189
 */

public class java_1111_sleep_yield {
    public static void main(String[] args) {

        // sleep()-yield()用法
        System.out.println("--------------------sleep()-yield()用法--------------------");

        // 创建线程
        MyThread6 thread6 = new MyThread6();           // 继承 Thread类 创建
        Thread thread7 = new Thread(new MyThread7());  // 实现 Runnable接口 创建

        // 启动线程
        thread6.start();
        thread7.start();

    }
}

// 创建线程方式一
class MyThread6 extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10000; ++i) {

            if (i == 2000) {
                // 调用 Thread.sleep() 使得线程睡眠
                try {

                    Thread.sleep(2000);  // 延迟 2 秒

                    System.out.println("调用 Thread.sleep() 使得线程睡眠 2 秒!");

                    Thread.sleep(1000);  // 延迟 1 秒

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            System.out.println("Thread1-(Thread): " + i);
        }
    }
}

// 创建线程方式一
class MyThread7 implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 10000; ++i) {
            System.out.println("Thread2-(Runnable): " + i);
        }
    }
}
